Namespace Game

    Module mGame

        Public IsInGame As Boolean

        Private ShopUpdateIsGrid As Boolean

        Public Sub Init()

            Windows.FocusInterface(Nothing)

            'If Constants.gSelectResX <> Constants.gGameResX Or Constants.gSelectResY <> Constants.gGameResY Then
            'DirectX.SetResoloution(Constants.gGameResX, Constants.gGameResY)
            'End If

            GameWindow.Init()
            Inventory.Init()
            Equipment.Init()
            Player.Init()
            InterfaceBar.Init()
            GameMenu.Init()
            ChatBox.Init()
            MiniMap.Init()
            SayWindow.Init()
            ShopWindow.Init()
            MagicWindow.Init()
            GroupWindow.Init()
            Belt.Init()

            Player.IsDead = False

            Globals.gGameState = Globals.GameState.Game

        End Sub

        Public Sub Shutdown()

            GameWindow.Shutdown()
            InterfaceBar.Shutdown()
            GameMenu.Shutdown()
            ChatBox.Shutdown()
            Inventory.Shutdown()
            Equipment.Shutdown()
            MiniMap.Shutdown()
            SayWindow.Shutdown()
            ShopWindow.Shutdown()
            MagicWindow.Shutdown()
            GroupWindow.Shutdown()
            Belt.Shutdown()

        End Sub

        Public Sub PacketArrival(ByRef info As EvoLib.Protocol.Packet, ByRef struct() As Byte)

            Dim tc As Long = DateTime.Now.Ticks

            If info.PacketType = EvoLib.Protocol.GamePacketType.EnterGame And info.PacketOpCode = EvoLib.Protocol.PacketOpCode.Notify Then
                IsInGame = True
                Globals.IsLoadingTiles = False
                MessageBoxes.RemoveStateNotice()
            End If

            Select Case CType(info.PacketType, EvoLib.Protocol.GamePacketType)

                Case EvoLib.Protocol.GamePacketType.DisconnectClient
                    Select Case info.PacketOpCode
                        Case EvoLib.Protocol.PacketOpCode.Notify
                            MessageBoxes.AddMessageBox("DISCONNECT", "Disconnected from server due to lack of socket activity!", MessageBoxes.MsgBoxButtons.OK, AddressOf MsgBoxResultCallback)
                        Case Else

                    End Select

                    'Case EvoLib.Protocol.GamePacketType.PlayerUpdate
                    '    Select Case info.PacketOpCode
                    'Case EvoLib.Protocol.PacketOpCode.Notify
                    '    Dim startinfo As StartInfo = ArrayToStruct(struct, GetType(StartInfo))
                    '    Player.Stand()
                    '    Player.SetPos(startinfo.PosInfo)
                    '    Player.PlayerLook = startinfo.BYTE_LOOK

                    'Case Else

                    'End Select

                Case EvoLib.Protocol.GamePacketType.PlayerMove
                    Select Case info.PacketOpCode
                        Case EvoLib.Protocol.PacketOpCode.Notify
                            Dim posinfo As EvoLib.Protocol.PositionInfo = EvoLib.Protocol.Functions.ArrayToStruct(struct, GetType(EvoLib.Protocol.PositionInfo))
                            Player.Stand()
                            If Not GameWindow.mCurrentMapFile = posinfo.STR_MAP Then
                                'GameWindow.LoadMap(posinfo.STR_MAP)
                                MessageBoxes.AddMessageBox("MSGBOX_MAP_MISMATCH", "PlayerMove EvoLib.Protocol.Packet that contained coordinates from a different map was recieved!", MessageBoxes.mMsgBoxVars.MsgBoxButtons.OK, AddressOf MsgBoxResultCallback)
                            End If
                            GameWindow.CoordX = posinfo.INT2_X
                            GameWindow.CoordY = posinfo.INT2_Y

                        Case Else

                    End Select

                Case EvoLib.Protocol.GamePacketType.CharacterAddUpdate
                    Select Case info.PacketOpCode
                        Case EvoLib.Protocol.PacketOpCode.Notify
                            If Not Globals.gGameState = Globals.GameState.Game Then Exit Sub
                            Dim charaddinfo As EvoLib.Protocol.CharacterAddInfo = EvoLib.Protocol.Functions.ArrayToStruct(struct, GetType(EvoLib.Protocol.CharacterAddInfo))
                            Characters.AddCharacter(charaddinfo)

                        Case Else

                    End Select

                Case EvoLib.Protocol.GamePacketType.MobAddUpdate
                    Select Case info.PacketOpCode
                        Case EvoLib.Protocol.PacketOpCode.Notify
                            If Not Globals.gGameState = Globals.GameState.Game Then Exit Sub
                            Dim mbaddinfo As EvoLib.Protocol.MobAddInfo = EvoLib.Protocol.Functions.ArrayToStruct(struct, GetType(EvoLib.Protocol.MobAddInfo))
                            Mobs.AddMob(mbaddinfo)
                        Case Else

                    End Select

                Case EvoLib.Protocol.GamePacketType.CharacterMoveUpdate
                    Select Case info.PacketOpCode
                        Case EvoLib.Protocol.PacketOpCode.Notify
                            Dim charmoveinfo As EvoLib.Protocol.CharacterMoveInfo = EvoLib.Protocol.Functions.ArrayToStruct(struct, GetType(EvoLib.Protocol.CharacterMoveInfo))
                            Characters.MoveCharacter(charmoveinfo.ID_CHARACTER, charmoveinfo.Positioninfo)

                        Case Else

                    End Select

                Case EvoLib.Protocol.GamePacketType.MobMoveUpdate
                    Select Case info.PacketOpCode
                        Case EvoLib.Protocol.PacketOpCode.Notify
                            Dim mobmoveinfo As EvoLib.Protocol.MobUpdateInfo = EvoLib.Protocol.Functions.ArrayToStruct(struct, GetType(EvoLib.Protocol.MobUpdateInfo))
                            Mobs.MoveMob(mobmoveinfo)

                        Case Else

                    End Select

                Case EvoLib.Protocol.GamePacketType.CharacterRemoveUpdate
                    Select Case info.PacketOpCode
                        Case EvoLib.Protocol.PacketOpCode.Notify
                            Dim charremoveinfo As EvoLib.Protocol.CharacterID = EvoLib.Protocol.Functions.ArrayToStruct(struct, GetType(EvoLib.Protocol.CharacterID))
                            Characters.RemoveCharacter(charremoveinfo.ID_CHARACTER)

                        Case Else

                    End Select

                Case EvoLib.Protocol.GamePacketType.MobRemoveUpdate
                    Select Case info.PacketOpCode
                        Case EvoLib.Protocol.PacketOpCode.Notify
                            Dim mobremoveinfo As EvoLib.Protocol.MobRemoveInfo = EvoLib.Protocol.Functions.ArrayToStruct(struct, GetType(EvoLib.Protocol.MobRemoveInfo))
                            Mobs.RemoveMob(mobremoveinfo.ServerMobID)

                        Case Else

                    End Select
                Case EvoLib.Protocol.GamePacketType.ChatMsg
                    Select Case info.PacketOpCode
                        Case EvoLib.Protocol.PacketOpCode.Notify
                            Dim msg As EvoLib.Protocol.CharChatMsg = EvoLib.Protocol.Functions.ArrayToStruct(struct, GetType(EvoLib.Protocol.CharChatMsg))
                            ChatBox.ChatIn(msg, tc)

                        Case Else

                    End Select

                Case EvoLib.Protocol.GamePacketType.MobChatMsg
                    Select Case info.PacketOpCode
                        Case EvoLib.Protocol.PacketOpCode.Notify
                            Dim msg As EvoLib.Protocol.MobChatMsg = EvoLib.Protocol.Functions.ArrayToStruct(struct, GetType(EvoLib.Protocol.MobChatMsg))
                            Mobs.ChatMessage(msg, tc)
                    End Select


                Case EvoLib.Protocol.GamePacketType.AddToInventory
                    Select Case info.PacketOpCode
                        Case EvoLib.Protocol.PacketOpCode.Notify
                            If Not Globals.gGameState = Globals.GameState.Game Then Exit Sub
                            Dim iteminfo As EvoLib.Protocol.ItemInfo = EvoLib.Protocol.Functions.ArrayToStruct(struct, GetType(EvoLib.Protocol.ItemInfo))
                            Inventory.ServerAddItem(iteminfo)
                        Case Else

                    End Select

                Case EvoLib.Protocol.GamePacketType.EquipItem
                    Select Case info.PacketOpCode
                        Case EvoLib.Protocol.PacketOpCode.Notify
                            Dim equipinfo As EvoLib.Protocol.ItemSlotInfo = EvoLib.Protocol.Functions.ArrayToStruct(struct, GetType(EvoLib.Protocol.ItemSlotInfo))
                            Equipment.EquipItem(equipinfo.item, equipinfo.slot)
                        Case Else

                    End Select

                Case EvoLib.Protocol.GamePacketType.PlayerHit
                    Select Case info.PacketOpCode
                        Case EvoLib.Protocol.PacketOpCode.Notify
                            Dim plyrhitinfo As EvoLib.Protocol.PlayerHitInfo = EvoLib.Protocol.Functions.ArrayToStruct(struct, GetType(EvoLib.Protocol.PlayerHitInfo))

                            'resurection code
                            If Player.IsDead And plyrhitinfo.VitalPoints.HP > 0 Then

                                'resurection [@resme gm command ONLY atm (03/05/2012)]
                                Player.VitalPoints = plyrhitinfo.VitalPoints
                                Player.IsDead = False
                                Player.ActionQueue.Clear()
                                System.Threading.Thread.Sleep(10)
                                Player.Stand()

                            Else

                                Dim diff As Integer = plyrhitinfo.VitalPoints.HP - Player.VitalPoints.HP

                                If diff <> 0 Or Not plyrhitinfo.IgnoreMiss Then
                                    AddHitIndicator(Player.lstHits, diff)
                                End If
                                Player.VitalPoints = plyrhitinfo.VitalPoints

                                If diff < 0 Then

                                    'normal player being hit code
                                    Player.Hit()

                                End If
                            End If

                        Case Else
                    End Select

                Case EvoLib.Protocol.GamePacketType.CharacterHit
                    Select Case info.PacketOpCode
                        Case EvoLib.Protocol.PacketOpCode.Notify
                            Dim hitinfo As EvoLib.Protocol.CharacterHitInfo = EvoLib.Protocol.Functions.ArrayToStruct(struct, GetType(EvoLib.Protocol.CharacterHitInfo))
                            Characters.HitCharacter(hitinfo)
                        Case Else
                    End Select

                Case EvoLib.Protocol.GamePacketType.MobHit
                    Select Case info.PacketOpCode
                        Case EvoLib.Protocol.PacketOpCode.Notify
                            Dim hitinfo As EvoLib.Protocol.MobHitInfo = EvoLib.Protocol.Functions.ArrayToStruct(struct, GetType(EvoLib.Protocol.MobHitInfo))
                            Mobs.HitMob(hitinfo)
                        Case Else
                    End Select

                Case EvoLib.Protocol.GamePacketType.Magic
                    Select Case info.PacketOpCode
                        Case EvoLib.Protocol.PacketOpCode.Notify
                            Dim addmagic As EvoLib.Protocol.AddMagic = EvoLib.Protocol.Functions.ArrayToStruct(struct, GetType(EvoLib.Protocol.AddMagic))
                            Magic.AddMagic(addmagic, False)
                    End Select

                Case EvoLib.Protocol.GamePacketType.PlayerStatsUpdate
                    Select Case info.PacketOpCode
                        Case EvoLib.Protocol.PacketOpCode.Notify
                            Player.Stats = EvoLib.Protocol.Functions.ArrayToStruct(struct, GetType(EvoLib.Protocol.StatsSet))

                    End Select

                Case EvoLib.Protocol.GamePacketType.CharacterShapeUpdate
                    Select Case info.PacketOpCode
                        Case EvoLib.Protocol.PacketOpCode.Notify
                            Dim charshape As EvoLib.Protocol.CharacterShape = EvoLib.Protocol.Functions.ArrayToStruct(struct, GetType(EvoLib.Protocol.CharacterShape))
                            Characters.CharacterShapeUpdate(charshape)
                    End Select

                Case EvoLib.Protocol.GamePacketType.CharacterDie
                    Select Case info.PacketOpCode
                        Case EvoLib.Protocol.PacketOpCode.Notify
                            Dim charid As EvoLib.Protocol.CharacterID = EvoLib.Protocol.Functions.ArrayToStruct(struct, GetType(EvoLib.Protocol.CharacterID))
                            Characters.CharacterDie(charid.ID_CHARACTER)
                    End Select

                Case EvoLib.Protocol.GamePacketType.LoadNewMap
                    Select Case info.PacketOpCode
                        Case EvoLib.Protocol.PacketOpCode.Notify

                            Dim loadmap As EvoLib.Protocol.LoadMapInfo = EvoLib.Protocol.Functions.ArrayToStruct(struct, GetType(EvoLib.Protocol.LoadMapInfo))
                            Characters.ClearList()
                            Mobs.ClearList()
                            NPCs.ClearList()
                            MiniMap.CurrentFmmapIndex = loadmap.Fmmap_Index
                            GameWindow.LoadMap(loadmap.Posinfo.STR_MAP)
                            GameWindow.mCurrentMapName = loadmap.STR_MAPNAME
                            GameWindow.CoordX = loadmap.Posinfo.INT2_X
                            GameWindow.CoordY = loadmap.Posinfo.INT2_Y
                    End Select

                Case EvoLib.Protocol.GamePacketType.Resync
                    Select Case info.PacketOpCode
                        Case EvoLib.Protocol.PacketOpCode.Answer
                            Dim startinfo As EvoLib.Protocol.StartInfo = EvoLib.Protocol.Functions.ArrayToStruct(struct, GetType(EvoLib.Protocol.StartInfo))
                            MiniMap.CurrentFmmapIndex = startinfo.LoadMapInfo.Fmmap_Index
                            GameWindow.LoadMap(startinfo.LoadMapInfo.Posinfo.STR_MAP)
                            GameWindow.CoordX = startinfo.LoadMapInfo.Posinfo.INT2_X
                            GameWindow.CoordY = startinfo.LoadMapInfo.Posinfo.INT2_Y
                            Player.ShapeInfo = startinfo.ShapeInfo
                            Player.VitalPoints = startinfo.VitalPoints

                        Case EvoLib.Protocol.PacketOpCode.Acknowledge
                            MessageBoxes.RemoveStateNotice()

                    End Select

                Case EvoLib.Protocol.GamePacketType.FloorItemAddUpdate
                    Select Case info.PacketOpCode
                        Case EvoLib.Protocol.PacketOpCode.Notify
                            Dim item As EvoLib.Protocol.FloorItem = EvoLib.Protocol.Functions.ArrayToStruct(struct, GetType(EvoLib.Protocol.FloorItem))
                            FloorItems.Add(item)
                    End Select

                Case EvoLib.Protocol.GamePacketType.FloorItemRemoveUpdate
                    Select Case info.PacketOpCode
                        Case EvoLib.Protocol.PacketOpCode.Notify
                            Dim itemid As Int64 = EvoLib.Protocol.Functions.ArrayToStruct(struct, GetType(Int64))
                            FloorItems.Remove(itemid)
                    End Select

                Case EvoLib.Protocol.GamePacketType.RemoveItem
                    Select Case info.PacketOpCode
                        Case EvoLib.Protocol.PacketOpCode.Notify
                            Dim itemid As Int64 = EvoLib.Protocol.Functions.ArrayToStruct(struct, GetType(Int64))
                            Inventory.mInventoryGrid.Remove(itemid)

                    End Select

                Case EvoLib.Protocol.GamePacketType.NPCSay
                    Select Case info.PacketOpCode
                        Case EvoLib.Protocol.PacketOpCode.Notify
                            Dim saystruct As EvoLib.Protocol.SayInfo = EvoLib.Protocol.Functions.ArrayToStruct(struct, GetType(EvoLib.Protocol.SayInfo))
                            SayWindow.Say(saystruct)
                    End Select

                Case GamePacketType.NPCCall
                    Select Case info.PacketOpCode
                        Case EvoLib.Protocol.PacketOpCode.NegAcknowledge
                            MessageBoxes.AddMessageBox("NPCCALL", "NPCCall failed!", MessageBoxes.MsgBoxButtons.OK, AddressOf Game.MsgBoxResultCallback)
                    End Select

                Case EvoLib.Protocol.GamePacketType.NPCAddUpdate
                    Select Case info.PacketOpCode
                        Case PacketOpCode.Notify
                            If Not Globals.gGameState = Globals.GameState.Game Then Exit Sub
                            Dim npcinfo As EvoLib.Protocol.NPCAddInfo = EvoLib.Protocol.Functions.ArrayToStruct(struct, GetType(EvoLib.Protocol.NPCAddInfo))
                            NPCs.AddNPC(npcinfo)
                    End Select

                Case EvoLib.Protocol.GamePacketType.SwitchServer
                    Select Case info.PacketOpCode
                        Case PacketOpCode.Notify
                            Dim serverinfo As EvoLib.Protocol.SwitchServer = EvoLib.Protocol.Functions.ArrayToStruct(struct, GetType(EvoLib.Protocol.SwitchServer))

                            Globals.gGameState = Globals.GameState.ServerSwitch
                            Game.IsInGame = False
                            Game.IsDoingServerSwitch = True

                            Globals.IsInGame = False
                            Game.Shutdown()

                            MessageBoxes.SetStateNotice("Connecting to " & serverinfo.STR_HOSTNAME & ":" & serverinfo.INT_PORT & "...")
                            ClientSock.ConnectTo(serverinfo.STR_HOSTNAME, CShort(serverinfo.INT_PORT))

                    End Select

                Case EvoLib.Protocol.GamePacketType.NPCShopItemsBeginUpdate
                    Select Case info.PacketOpCode
                        Case PacketOpCode.Notify
                            Dim shopinfo As EvoLib.Protocol.ShopInfo = EvoLib.Protocol.Functions.ArrayToStruct(struct, GetType(EvoLib.Protocol.ShopInfo))
                            If Not shopinfo.BOOL_GRIDLIST Then ShopWindow.Hide()
                            ShopWindow.StartDef(shopinfo.ID_NPC, shopinfo.BOOL_GRIDLIST)
                            ShopUpdateIsGrid = shopinfo.BOOL_GRIDLIST
                    End Select

                Case EvoLib.Protocol.GamePacketType.NPCShopItemsEndUpdate
                    Select Case info.PacketOpCode
                        Case PacketOpCode.Notify
                            If ShopUpdateIsGrid Then
                                ShopWindow.EndItemsUpdate()
                            Else
                                ShopWindow.SetMode(ShopWindow.ShopMode.Main)
                                ShopWindow.Show()
                            End If
                    End Select

                Case EvoLib.Protocol.GamePacketType.NPCShopItemsAdd
                    Select Case info.PacketOpCode
                        Case PacketOpCode.Notify
                            Dim shopiteminfo As EvoLib.Protocol.ShopItemInfo = EvoLib.Protocol.Functions.ArrayToStruct(struct, GetType(EvoLib.Protocol.ShopItemInfo))
                            If ShopUpdateIsGrid Then
                                ShopWindow.AddItem(shopiteminfo)
                            Else
                                ShopWindow.AddDef(shopiteminfo)
                            End If
                    End Select

                Case EvoLib.Protocol.GamePacketType.NPCShopBeginAllowList
                    Select Case info.PacketOpCode
                        Case PacketOpCode.Notify
                            Dim listinfo As EvoLib.Protocol.ShopListInfo = EvoLib.Protocol.Functions.ArrayToStruct(struct, GetType(EvoLib.Protocol.ShopListInfo))
                            Inventory.AllowListType = listinfo.BYTE_TYPE
                            Inventory.NPC_ID = listinfo.ID_NPC
                            Inventory.ClearAllowList()

                    End Select

                Case EvoLib.Protocol.GamePacketType.NPCShopAllow
                    Select Case info.PacketOpCode
                        Case PacketOpCode.Notify
                            Dim allow As ShopAllowList = EvoLib.Protocol.Functions.ArrayToStruct(struct, GetType(ShopAllowList))
                            Inventory.AddAllow(allow.BYTE_TYPE)
                    End Select

                Case EvoLib.Protocol.GamePacketType.NPCShopEndAllowList
                    Select Case info.PacketOpCode
                        Case PacketOpCode.Notify
                            Inventory.EndAllowList()
                    End Select

                Case EvoLib.Protocol.GamePacketType.SetCurrency
                    Select Case info.PacketOpCode
                        Case PacketOpCode.Notify
                            Dim currency As CurrencyInfo = EvoLib.Protocol.Functions.ArrayToStruct(struct, GetType(CurrencyInfo))
                            Dim line As ChatBox.ChatLine
                            line.Type = EvoLib.Protocol.mProtocol.ChatMsgType.System
                            If currency.BYTE_TYPE = CurrencyType.Gold Then
                                If Not (currency.INT8_AMMOUNT = 0) Then
                                    If currency.INT8_AMMOUNT > 0 Then
                                        line.Msg = "Recieved " & currency.INT8_AMMOUNT & " gold"
                                    Else
                                        line.Msg = "Spent " & (0 - currency.INT8_AMMOUNT) & " gold"
                                    End If
                                    ChatBox.AddChatLine(line)
                                End If
                                Player.Gold = currency.INT8_TOTAL
                            ElseIf currency.BYTE_TYPE = CurrencyType.GameGold Then
                                If Not (currency.INT8_AMMOUNT = 0) Then
                                    If currency.INT8_AMMOUNT > 0 Then
                                        line.Msg = "Recieved " & currency.INT8_AMMOUNT & " gamegold"
                                    Else
                                        line.Msg = "Spent " & (0 - currency.INT8_AMMOUNT) & " gamegold"
                                    End If
                                    ChatBox.AddChatLine(line)
                                End If
                                Player.GameGold = currency.INT8_TOTAL
                            ElseIf currency.BYTE_TYPE = CurrencyType.FamePoints Then
                                If Not (currency.INT8_AMMOUNT = 0) Then
                                    If currency.INT8_AMMOUNT > 0 Then
                                        line.Msg = "Recieved " & currency.INT8_AMMOUNT & " gold"
                                    Else
                                        line.Msg = "Spent " & (0 - currency.INT8_AMMOUNT) & " gold"
                                    End If
                                    ChatBox.AddChatLine(line)
                                End If
                                Player.FamePoints = currency.INT8_TOTAL
                            End If
                    End Select

                Case GamePacketType.NPCShopSell
                    Select Case info.PacketOpCode
                        Case PacketOpCode.Answer
                            Dim sellinfo As NPCShopSellPrice = Protocol.Functions.ArrayToStruct(struct, GetType(NPCShopSellPrice))
                            Inventory.SellInfo(sellinfo)
                    End Select

                Case GamePacketType.SetExp
                    Select Case info.PacketOpCode
                        Case PacketOpCode.Notify
                            Dim exp As ExpInfo = Protocol.Functions.ArrayToStruct(struct, GetType(ExpInfo))

                            Player.PlayerLevel = exp.INT2_LEVEL
                            Player.CurrentXP = exp.INT8_EXP
                            Player.NextLevelXP = exp.INT8_TOTALEXP

                    End Select

                Case GamePacketType.AddPlayerSkill
                    Select Case info.PacketOpCode
                        Case PacketOpCode.Custom2
                            'clear the magics list
                            MagicWindow.ClearSkillsList()
                        Case PacketOpCode.Notify
                            Dim skill As PlayerSkill = Protocol.Functions.ArrayToStruct(struct, GetType(PlayerSkill))
                            MagicWindow.AddSkill(skill)
                        Case PacketOpCode.Custom3
                            Dim updatedesc As PlayerSkillDesc = Protocol.Functions.ArrayToStruct(struct, GetType(PlayerSkillDesc))
                            MagicWindow.UpdateSkill(updatedesc)

                    End Select

                Case GamePacketType.FormGroup
                    Select Case info.PacketOpCode
                        Case PacketOpCode.Notify
                            Dim tmp As GroupID = Protocol.Functions.ArrayToStruct(struct, GetType(GroupID))
                            GroupWindow.SetGrpID(tmp)

                    End Select

                Case GamePacketType.AddToGrp
                    Select Case info.PacketOpCode
                        Case PacketOpCode.Notify
                            Dim tmp As GroupNameID = Protocol.Functions.ArrayToStruct(struct, GetType(GroupNameID))
                            GroupWindow.AddGrpPkt(tmp)

                    End Select

                Case GamePacketType.RemoveFrmGrp
                    Select Case info.PacketOpCode
                        Case PacketOpCode.Notify
                            Dim tmp As GroupID = Protocol.Functions.ArrayToStruct(struct, GetType(GroupID))
                            GroupWindow.RemoveGrpPkt(tmp)

                    End Select

                Case GamePacketType.UpdatePlayerWeights
                    Select Case info.PacketOpCode
                        Case PacketOpCode.Notify
                            Dim tmp As PlayerWeightDesc = Protocol.Functions.ArrayToStruct(struct, GetType(PlayerWeightDesc))
                            'check if this packet relates to this client/player
                            If tmp.ID_CHARACTER = Player.PlayerID Then
                                'update about this player
                                Player.PlayerWeight = tmp.Weights
                            Else
                                'update about another player
                                Characters.UpdateCharacterWeight(tmp)
                            End If
                    End Select

            End Select

        End Sub

        Public Sub MsgBoxResultCallback(ByRef result As MessageBoxes.MsgBoxResult)

            Select Case result.mName

                Case "MSGBOX_MAP_MISMATCH"
                    Globals.bEnding = True

                Case "InventoryError"
                    If result.mResult = MessageBoxes.MsgBoxResults.Yes Then
                        Dim mb As MessageBoxes.MsgBoxStruct
                        Do
                            If MessageBoxes.mMsgBoxes.Count = 0 Then Exit Do
                            mb = MessageBoxes.mMsgBoxes.Peek
                            If mb.mName = "InventoryError" Then
                                MessageBoxes.mMsgBoxes.Dequeue()
                            Else
                                Exit Do
                            End If
                        Loop
                    End If

                Case "ResyncQ"
                    If result.mResult = MessageBoxes.MsgBoxResults.Yes Then
                        DoResync()
                    End If

                Case "DROP"
                    If result.mResult = MessageBoxes.MsgBoxResults.Yes Then
                        ''Drop
                        Dim pkt As EvoLib.Protocol.Packet
                        pkt.PacketType = EvoLib.Protocol.GamePacketType.DropItem
                        pkt.PacketOpCode = EvoLib.Protocol.mProtocol.PacketOpCode.Enquiry
                        pkt.Username = Globals.sUsername

                        Dim struct As EvoLib.Protocol.DropItemRequest
                        struct.INT_ID = GUI.MouseDragItem.MouseDragItemDesc.info.INT_ID

                        ClientSock.SendData(EvoLib.Protocol.MakePacket(pkt, struct, Globals.Crypto))

                        GUI.MouseDragItem.RemoveMouseDragItem()

                    Else

                        'Return
                        GUI.MouseDragItem.ReturnMouseDragItem()
                        GUI.MouseDragItem.RemoveMouseDragItem()

                    End If

                Case "DISCONNECT"
                    ClientSock.gSock.Disconnect()
                    Application.DoEvents()
                    Globals.bEnding = True

                Case "SPLITSTACK"

                    'check input is numerical
                    If IsNumeric(result.mInput) Then

                        Dim size As Integer = result.mInput

                        'check requested stack size is more than 0 therfore valid
                        If size > 0 Then

                            'send packet to the server requesting the stack be split

                            Dim req As Protocol.SplitStackRequest
                            req.INT_ID = Inventory.SplittingStack
                            req.INT_STACKSIZE = size
                            Dim pkt As Packet
                            pkt.PacketType = GamePacketType.SplitStack
                            pkt.PacketOpCode = EvoLib.Protocol.mProtocol.PacketOpCode.Enquiry
                            pkt.Username = Globals.sUsername

                            ClientSock.gSock.SendData(MakePacket(pkt, req, Globals.Crypto))

                        End If
                    End If

            End Select

        End Sub

        Public Sub PlayerInputMouseCheck(ByRef type As Mouse.mMouseVars.MouseInputType, ByRef e As System.Windows.Forms.MouseEventArgs)
            If type = Mouse.MouseInputType.MouseMove And (e.Button = MouseButtons.Left Or e.Button = MouseButtons.Right) And Player.currentanim <> EvoLib.Protocol.HUMAnimation.Standing Then
                Player.Stand()
            End If
        End Sub

        Public Enum VitalsStyle
            HUM_HP_Only = 0
            HUM_Both = 1
            MOB_HP_Only = 2
        End Enum

        Public Sub DrawVitalsBar(ByVal x As Integer, ByVal y As Integer, ByVal Vitals As EvoLib.Protocol.Vitals, Optional style As VitalsStyle = VitalsStyle.HUM_Both)

            Dim dx As Integer = (x + ((Constants.TileWidth - Libs.GameInter.Image(4).Header.Width) \ 2))
            Dim dy As Integer = (y - (Constants.TileHeight * 1.9))
            Dim z As Single = Constants.VitalsBarZ

            'Mob Style GREEN HP Bar
            If style = VitalsStyle.MOB_HP_Only Then

                'Draw the Outline of the HP Bar
                Libs.GameInter.Image(4).Draw(dx, dy, z)

                'Draw the HP Bar
                With Libs.GameInter.Image(5)
                    Dim hpW As Integer
                    If Vitals.MAXHP > 0 Then hpW = (CSng(.Header.Width) * (CSng(Vitals.HP) / CSng(Vitals.MAXHP)))
                    DirectX.BlitD3D(.Texture, dx + 1, dy + 1, hpW, .Header.Height, z)
                End With

            End If

            'HUM Style RED HP Bar
            If style = VitalsStyle.HUM_HP_Only Or style = VitalsStyle.HUM_Both Then

                'Draw the Outline of the HP Bar
                Libs.GameInter.Image(4).Draw(dx, dy, z)

                'Draw the HP Bar
                With Libs.GameInter.Image(2)
                    Dim hpW As Integer
                    If Vitals.MAXHP > 0 Then hpW = (CSng(.Header.Width) * (CSng(Vitals.HP) / CSng(Vitals.MAXHP)))
                    DirectX.BlitD3D(.Texture, dx + 1, dy + 1, hpW, .Header.Height, z)
                End With

            End If

            'HUM Style Blue MP Bar
            If style = VitalsStyle.HUM_Both Then

                'Draw the Outline of the HP Bar
                With Libs.GameInter.Image(4)
                    .Draw(dx, dy + (.Header.Height - 1), z)
                End With

                'Draw the MP Bar
                With Libs.GameInter.Image(3)
                    Dim mpW As Integer
                    If Vitals.MAXMP > 0 Then mpW = (CSng(.Header.Width) * (CSng(Vitals.MP) / CSng(Vitals.MAXMP)))
                    DirectX.BlitD3D(.Texture, dx + 1, dy + (Libs.GameInter.Image(4).Header.Height), mpW, .Header.Height, z)
                End With

            End If

        End Sub

        Public Sub AskToResync()

            MessageBoxes.AddMessageBox("ResyncQ", "Are you sure you wish to re-sync with the server?", MessageBoxes.MsgBoxButtons.YesNo, AddressOf MsgBoxResultCallback)

        End Sub

        Public Sub DoResync()

            MessageBoxes.SetStateNotice("Please wait, Resynchronizing...")

            'erase all item data
            Equipment.Reset()
            Inventory.Reset()

            'ask server to resync
            Dim pkt As EvoLib.Protocol.Packet
            pkt.PacketType = EvoLib.Protocol.GamePacketType.Resync
            pkt.PacketOpCode = EvoLib.Protocol.PacketOpCode.Enquiry
            pkt.Username = Globals.sUsername

            ClientSock.SendData(EvoLib.Protocol.MakePacket(pkt, , Globals.Crypto))

        End Sub

    End Module

End Namespace